
<template>
  <div class="details">
    <!--顶部导航栏  -->
    <van-nav-bar
      title="详情"
      left-text="返回"
      right-text="..."
      left-arrow
      @click-left="$router.go(-1)"
    />
    <!-- 店铺头 -->
    <div
      class="shophead"
      :style="{
        background:
          'url(' + (shopinfo.info && shopinfo.info.poi_back_pic_url) + ')',
      }"
    >
      <div class="title">
        <img :src="shopinfo.info && shopinfo.info.picUrl" alt="" />
        <span class="name">{{ shopinfo.info && shopinfo.info.name }}</span>
      </div>
      <!-- 点击取消收藏 -->
      <span
        class="Collection"
        v-if="isCollect"
        @click="request_unfav(shopinfo.info)"
        style="color: red"
        >❤</span
      >
      <!-- 收藏按钮  v-else-->
      <span class="Collection" v-else @click="request_fav(shopinfo.info)">
        ❤
      </span>
    </div>
    <!-- 商品列表 -->
    <div class="box">
      <div class="left">
        <van-sidebar v-model="currentIndex">
          <van-sidebar-item
            v-for="(item, index) in shopinfo.taglist"
            :key="index"
            :title="item.name"
          />
        </van-sidebar>
      </div>
      <div class="right">
        <div
          class="good"
          v-for="(item, index) in shopinfo.taglist &&
          shopinfo.taglist[currentIndex].goodlist"
          :key="index"
        >
          <div class="imgbox">
            <img :src="item.picture" alt="" />
          </div>
          <div class="text">
            <div class="up">
              <div class="name">{{ item.name }}</div>
              <div class="desc">{{ item.month_saled_content }}</div>
            </div>
            <div class="down">
              <div class="price">¥{{ item.min_price }}</div>
              <div class="opes">
                <!-- 判断商品是否已经存在与购物车来控制商品的显示隐藏 -->
                <span
                  v-show="isInshopcart(item)"
                  @click="request_shopcart_sub(item)"
                  >-</span
                >
                <span v-show="isInshopcart(item)">
                  {{ isInshopcartcount(item) }}
                </span>
                <span @click="request_shopcart_add(item)">+</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <Shopcart ref="shopcart" />
    <!-- 购物车 -->
    <div class="goods">
      <div class="left">
        <img
          src="https://gw.alicdn.com/imgextra/i2/O1CN01uGE6Bn1m4FQ1OiPfF_!!6000000004900-2-tps-249-249.png"
          alt=""
          @click.stop="$refs.shopcart.changeVisible()"
        />
        <span>总计￥{{ money() }}</span>
      </div>
      <div class="right" @click="money">去结算</div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapGetters, mapActions } from "vuex";

import Shopcart from "../components/Shopcart.vue";
import { shopinfo } from "../utils/api";
import { Dialog } from "vant";
export default {
  components: { Shopcart },
  data() {
    return {
      currentIndex: 0,
      shopinfo: {},
    };
  },
  computed: {
    ...mapState(["collectlist", "shopcartlist"]),
    ...mapGetters([]),
    isCollect() {
      var index = this.collectlist.findIndex((item) => {
        return item.mtWmPoiId == this.shopinfo.mtWmPoiId;
      });
      return index !== -1;
    },
    // 计算总价
    Totalprice() {
      let money = this.shopcartlist;
    },
  },
  methods: {
    // 结算
    money() {
      let money = 0;
      this.shopcartlist.forEach((element) => {
        money += element.count * element.min_price;
        // console.log("12" + element.count * element.min_price);
      });
      return money.toFixed(2);
    },
    // 同步
    ...mapMutations(["collect", "cancel_collect"]),
    // 异步
    ...mapActions([
      "request_fav",
      "request_unfav",
      "request_shopcart_add",
      "request_shopcart_sub",
    ]),
    // 判断是否在购物车
    isInshopcart(item) {
      let index = this.shopcartlist.findIndex((it) => {
        return it.id == item.id;
      });
      return index !== -1;
    },
    //
    isInshopcartcount(item) {
      let good = this.shopcartlist.find((it) => {
        return it.id == item.id;
      });
      if (good) {
        return good.count;
      } else {
        return 0;
      }
    },
  },
  mounted() {
    // 发请求获取店铺信息
    shopinfo({ id: this.$route.params.id }).then((res) => {
      if (res.data.shopinfo) {
        //店铺正常营业
        this.shopinfo = res.data.shopinfo;
      } else {
        //店铺倒闭
        Dialog.alert({
          title: "提示",
          message: "该店铺已倒闭!",
        }).then(() => {
          this.$router.go(-1);
        });
      }
    });
  },
};
</script>

<style lang="scss" scoped>
.shophead {
  padding: 50px 10px;
  margin: 0 5px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .title {
    display: flex;
    align-items: center;
    img {
      width: 20px;
      height: 20px;
      border-radius: 50%;
      margin: 5px;
    }
    .name {
      color: #fff;
    }
    span {
      margin: 0 10px;
    }
  }

  .Collection {
    font-size: 24px;
  }
}
.box {
  display: flex;
  .left {
    width: 80px;
  }
  .right {
    flex: 1;
    .good {
      display: flex;
      margin: 10px;
      .imgbox {
        width: 80px;
        margin-right: 10px;
        img {
          width: 100%;
        }
      }
      .text {
        flex: 1;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .down {
          display: flex;
          justify-content: space-between;
        }
        .opes {
          height: 20px;
          width: 80px;
          display: flex;
          justify-content: flex-end;
          span {
            width: 20px;
            display: inline-block;
            background: rgb(25 124 255);
            border-radius: 50%;
            line-height: 20px;
            font-size: 14px;
            color: #fff;
            text-align: center;
            margin: 0 3px;
          }
        }
      }
    }
  }
}
.goods {
  height: 50px;
  width: 100%;
  background-color: #555;
  position: fixed;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  .left {
    display: flex;
    align-items: center;
    flex: 1;
    img {
      height: 80px;
      width: 80px;
      margin-bottom: 50px;
    }
    span {
      color: aliceblue;
      font-weight: bold;
      line-height: 50px;
    }
  }
  .right {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    align-self: center;
    color: #fff;
    background-color: limegreen;
    margin-right: 20px;
    border-radius: 5px;
  }
}
</style>